import pymysql;
import time;
import datetime;
from org.model.entity_model import DeskZol;



def dbconnect():

    return pymysql.connect(host="127.0.0.1", user="root", password="12345", db="crawl_image", port=3306, charset='utf8mb4',
                           cursorclass=pymysql.cursors.DictCursor);

def insertZol(label,url,disk):

    db = dbconnect();

    try:
        with db.cursor() as cursor:
            sql = "insert into desk_zol(label,url,disk,ctime)values(%s,%s,%s,%s)";
            dt = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            cursor.execute(sql,(label,url,disk,dt));

            cursor.close();
    except Exception as e:
        print(e)
        db.rollback(); #如果发生一次回滚事务
        db.close();
    else:
        print("提交")
        db.commit();

    pass;




def select():
    db = dbconnect();
    cursor = db.cursor();
    cursor.execute("select * from desk_zol");

    rs = cursor.fetchall()

    for row in rs:
        deskZol = DeskZol(row);
        print(deskZol.getUrl())
    cursor.close();
    db.close();
    pass;

select();







